5.3 Initializing the C Runtime Environment
The final instruction of the start.S utility is an unconditional branch command jumping to _main, forcing the CPU to jump to the _main entry point declared within crt0.S to process subsequent logic.
Before transitioning into the C programming execution state, the assembly logic in crt0.S coordinates the low-level, foundational allocation of resources. It first loads the absolute baseline starting address of the global_data structure into a dedicated processing register (specifically register r9 on ARM 32-bit architectures) and provisions the primary Stack Pointer (sp) required for C runtime execution, allowing downstream C functions to access parameters directly via gd->xxx. By invoking board_init_f_alloc_reserve(), it calculates and carves out the explicit memory margins required to accommodate the early stack frame and the gd tracking structure. It then invokes board_init_f_init_reserve() to clear and zero out the newly isolated gd memory region, ensuring all constituent structural members initialize cleanly to zero. Finally, it triggers board_init_f() to dynamically assign values to the internal tracking fields of the global_data layout.
The stack layout coordinates utilized by U-BOOT Proper align with the parameter CONFIG_SYS_INIT_SP_ADDR, whereas the SPL layer targets CONFIG_SPL_STACK and the TPL layer maps to CONFIG_TPL_STACK. These macro definitions reside inside the platform include files located within the directory tree git/include/configs/. For the NXP i.MX6 applications processor, the explicit parameter boundary for CONFIG_SPL_STACK is defined within the imx6_spl.h include file.
Within the U-BOOT architecture, the struct global_data structure (universally abbreviated as gd) acts as the primary global data repository tracking the platform lifecycle across the entire system boot sequence. Its core architectural declaration is maintained within the generic source file git/include/asm-generic/global_data.h. Because U-BOOT lacks an available global static variable block prior to executing the code relocation sequence (as the uninitialized data block, or BSS segment, remains completely uninitialized at this stage), the system routes this pointer tracking structure across active function boundaries by anchoring it within a dedicated processor register or specialized stack offset.
This global tracking framework encapsulates an array of metrics used to register physical hardware parameters, volatile memory distributions, and code relocation layout parameters, serving as the primary bridge to pass runtime data tables from the pre-relocation phase (board_init_f()) over to the post-relocation phase (board_init_r()). The primary structural members of this framework include:
-
bd_t *bd: Points to the platform-specific board information structure (board info), containing baseline hardware attributes such as memory starting addresses and bank capacities.
-
unsigned long flags: Status bitmask flags identifying the active runtime state of the system (such as tracking whether components are fully initialized, relocated, or running in silent mode).
-
unsigned int baudrate: Registers the active data transfer speed (baud rate) assigned to the primary serial console.
-
unsigned long relocaddr: Identifies the absolute destination target address in physical memory where the U-BOOT binary is relocated.
-
unsigned long reloc_off: Stores the explicit linear offset tracking the delta between the actual running address execution vectors and the default compile-time link addresses.
-
const void *fdt_blob: Stores the absolute physical memory address hosting the Flattened Device Tree binary (dtb) blob.
-
struct udevice *cur_serial_dev: Points directly to the structural driver instance managing the active serial communication port.
The crt0.S file resides within the directory tree git/arch/arm/lib/. The low-level assembly execution pipeline tracking the initialization sequence inside crt0.S is mapped out in the flowchart below:
Figure 5-5 crt0.S Execution Flowchart
The pre-relocation routine board_init_f() resides within the file git/common/board_f.c and acts as the universal hardware setup engine for standard U-BOOT Proper boot pathways. It invokes initcall_run_list() to execute a sequential array of initialization calls mapped out within the init_sequence_f array, which is declared inside board_f.c. Regulated by preprocessor build flags, init_sequence_f aggregates a wide array of architecture-specific and feature-dependent hardware setup loops. System architects choose the appropriate initialization sequence to match target hardware envelopes by toggling these configuration switches.
This execution array is divided into four distinct systemic phases: execution environment construction, core hardware initialization, memory allocation layout planning, and final pre-relocation preparation.
The Execution Environment Construction Phase relies primarily on the following functions:
-
setup_mon_len: Calculates the absolute linear footprint and size of the compiled U-BOOT binary image to prepare subsequent memory allocation and relocation tracking blocks.
-
fdtdec_setup: Sets up and validates the internal Device Tree Blob (DTB), exposing the baseline peripheral properties and target register base addresses (such as console UART base parameters) to the bootloader.
-
initf_malloc: Inits a lightweight, early memory allocation pool (Early Malloc). Because formal memory management and dynamic heap allocators are not yet active, this early heap block is required to manage temporary object instances.
-
initf_dm: Initialized the unified Driver Model (DM) architecture, bringing up U-BOOT's core device driver framework to enable the system to find and bind driver abstractions by name.
The Core Hardware Initialization Phase relies primarily on the following functions:
-
arch_cpu_init / mach_cpu_init: Coordinates low-level System-on-Chip (SoC) register configurations, tuning internal bus clocks and silencing asynchronous hardware blocks that could interfere with early boot routines.
-
timer_init: Initialized the low-level system hardware timer, ensuring microsecond delays (udelay()) and communication timeout checks compute deterministically.
-
env_init: Evaluates persistent storage links to resolve the exact physical location of the environment variables block (identifying whether parameters reside on an SD card, flash memory blocks, or fall back to default read-only memory arrays).
-
serial_init / console_init_f: A highly critical step that initializes the primary serial port driver, enabling text streams to output cleanly onto the host console terminal.
-
dram_init: A milestone function that handles volatile memory discovery. Within an SPL/TPL build, this routine handles the heavy lifting of tuning timing registers and bringing up the external memory controller to make DDR RAM usable. Within a Proper build, it typically reads pre-existing registers or parameters from the DTB to verify bank allocations.
The Memory Allocation Layout Planning Phase relies primarily on the following functions:
-
reserve_mmu: Reserves memory blocks to house the core MMU page tables, preparing the platform to enable cache engines to accelerate execution speeds.
-
reserve_uboot: Allocates the exact destination memory boundaries needed to accommodate the relocated U-BOOT binary image.
-
reserve_global_data: Carves out physical memory to hold the post-relocation global_data (gd) tracking structure.
-
reserve_stacks: Standardizes the absolute layout coordinates and memory margins required to anchor the main C execution stack.
The Final Pre-Relocation Preparation Phase relies primarily on the following functions:
-
setup_reloc: Sets the absolute target destination coordinates in physical RAM where the U-BOOT binary image will be copied.
-
clear_bss: Clears the uninitialized data section (BSS segment) by filling it with zeroes. At this stage, the system has successfully established a complete C runtime environment (the execution stacks are ready, the heap is operational, and the BSS block is cleanly initialized).
-
jump_to_copy (along with subsequent assembly routines): Within a U-BOOT Proper pipeline, this routine coordinates copying the active binary from flash storage into external DDR memory, then branches execution over to the relocated address space. Within an SPL or TPL pipeline, although the system traverses this identical initialization array, the board_init_r() phase typically bypasses relocation to locate and load the subsequent stage binary image.
For the NXP i.MX6 family of applications processors, when compiled under an SPL configuration to minimize the memory footprint, the universal board_init_f() sequence is replaced with a highly optimized, simplified execution block maintained within the board file git/board/freescale/mx6sabresd/mx6sabresd.c. Under this setup, the streamlined initialization sequence is defined as:
Figure 5-6 board_init_f Function Flowchart
Depending on the chosen boot mode, crt0.S provides distinct initialization pathways. The Proper boot method requires shifting the execution location of the U-BOOT code within DDR memory; the invoked subroutines relocate_code and relocate_vectors are defined in the file relocate.S. Conversely, the SPL boot method requires relocating the active stack frame; the tracking stack relocation routine for SPL is located within the file git/common/spl.c.
The explicit function of relocate_code is to copy the contiguous code segment bounded between the symbols __image_copy_start and __image_copy_end into the physical RAM destination address pointed to by register r0. This copied section corresponds directly to the built-in.o object artifacts generated across the U-BOOT source tree, which represent the exact code blocks currently active in memory. The relocate_vectors subroutine is subsequently invoked to shift the system exception vectors to their designated runtime base.
The physical layout mapping U-BOOT memory allocation is structured as shown in the diagram below:
Figure 5-7 U-BOOT Memory Allocation
Figure 5-8 U-BOOT Memory Allocation After Code Relocation
Based on the low-level configuration of the CPU core, relocate_vectors remaps the exception vectors either to base address 0x00000000 or to high address 0xFFFF0000. The ARMv7 architecture natively allows exception vectors to reside at either the low base (0x00000000) or the high base (0xFFFF0000); this boundary choice is governed explicitly by the V bit within register c1 of the CP15 coprocessor space. The updated structural layout of the exception vectors within memory is shown below:
Figure 5-9 ARM Exception Vector Mapping Locations
If the system executes under a U-BOOT Proper boot pipeline, the routine subsequently invokes c_runtime_cpu_setup to invalidate the internal instruction cache (if the instruction cache has been explicitly enabled).
The code path directly following the execution of c_runtime_cpu_setup handles both the standard U-BOOT Proper sequence and any complex SPL pipelines that explicitly toggle the CONFIG_SPL_FRAMEWORK compilation flag. When an SPL implementation grows in complexity to support advanced subsystems, enabling the CONFIG_SPL_FRAMEWORK abstraction layer becomes structurally mandatory. The core objective of this final assembly block is to execute runtime environment cleanup, optionally re-anchor the active Stack Pointer and the global_data (gd) base tracking pointer, and execute the definitive jump into the post-relocation C entry function, board_init_r.